<template>
  <el-table :data="tableData" :border="true" style="width: 100%" row-key="id">
    <el-table-column label="名称" prop="name" />
    <el-table-column label="权限值" prop="code" />
    <el-table-column label="修改时间" prop="updateTime" />

    <el-table-column label="操作" prop="name">
      <template #="{ row, $index }">
        <el-button
          v-has="'btn.Permission.add'"
          type="primary"
          @click="addFunction(row)"
          :disabled="row.level == 4 ? true : false"
        >
          {{ row.level == 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          type="primary"
          @click="updatePermission(row)"
          v-has="'btn.Permission.update'"
          :disabled="row.level == 1 ? true : false"
        >
          编辑
        </el-button>

        <el-popconfirm title="确定删除吗" @confirm="deleteMenu(row)">
          <template #reference>
            <el-button
              type="primary"
              v-has="'btn.Permission.remove'"
              :disabled="row.level == 1 ? true : false"
            >
              删除
            </el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog
    v-model="dialogFormVisible"
    :title="form.id ? '更新菜单' : '添加菜单'"
    width="500"
  >
    <el-form :model="form">
      <el-form-item label="名称" :label-width="formLabelWidth">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限值" :label-width="formLabelWidth">
        <el-input v-model="form.code" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmUpdate()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {
  reqAddPremission,
  reqAllPremission,
  reqDeleteMenu,
} from '@/api/acl/menu'
import { IPermission, IPermissionParam } from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'

const tableData = ref<IPermission[]>([])

const dialogFormVisible = ref(false)
const formLabelWidth = '140px'

const form = reactive<IPermissionParam>({
  name: '',
  code: '',
  id: '',
  level: 0,
  pid: '',
})

const clearForm = () => {
  Object.assign(form, {
    name: '',
    code: '',
    id: '',
    level: 0,
    pid: '',
  })
}
const deleteMenu = async (data) => {
  try {
    let result = await reqDeleteMenu(data.id)
    if (result.code == 200) {
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      getAllMenuFromServer()
    }
  } catch (error) {
    console.log(error)
  }
}

const getAllMenuFromServer = async () => {
  try {
    let result = await reqAllPremission()
    if (result.code == 200) {
      tableData.value = result.data
    }
  } catch (error) {
    console.log(error)
  }
}
const addFunction = (data) => {
  console.log(data)

  clearForm()
  form.pid = data.id
  form.level = data.level + 1
  dialogFormVisible.value = true
}

const updatePermission = (data) => {
  Object.assign(form, data)
  dialogFormVisible.value = true
  console.log(form)
}
const confirmUpdate = async () => {
  console.log(form)
  if (form.name.trim() && form.code.trim()) {
    try {
      let result = await reqAddPremission(form)
      if (result.code == 200) {
        ElMessage({
          type: 'success',
          message: '成功',
        })
        dialogFormVisible.value = false
        getAllMenuFromServer()
      }
    } catch (error) {
      console.log(error)
    }
  } else {
    ElMessage({
      type: 'error',
      message: '两项不能为空',
    })
  }
}

onMounted(() => {
  getAllMenuFromServer()
})
</script>

<style lang="scss" scoped></style>
